//系统管理成员
"use strict"
const mvc = require('cosjs.mvc');
const dbase = require('../dbase');
const crypto = require('crypto');
const secret = 'szmzbzbzlp';

class members extends dbase.mongodb {
    constructor(){
        super('members');
        this.ObjectID = ObjectID.bind(this);
    }


    exist(guid){
        let query = {"guid":guid};
        return this.count(query);
    }
    //登录验证
    getFromName(guid,keys){
        let query = this.query(guid,'guid');
        let option = {multi:this.isMulti(guid), fields:this.fields(keys) };
        return this.find(query,option);
    }
    //密码加密
    password(str){
        let pwd = members_password(str,secret);
        if(typeof arguments[1] === 'function'){
            arguments[1](null,pwd)
        }
        else{
            return pwd;
        }
    }
}

function members_password(str){
    var cipher  = crypto.createCipher('aes-128-cfb',arguments[1]||secret);
    var enc  = cipher.update(str, "utf8", "hex");
    enc += cipher.final("hex");
    return mvc.library("crypto/md5",enc);
}



function ObjectID(id){
    if(id){
        return id;
    }
    else{
        return parseInt(Date.now() / 1000).toString(32);
    }
}

module.exports =  function(){
    return new members();
}
module.exports.password = members_password;

